<?php
/**
 * Cookie管理类
 */

class Cookie {

	/**
	 * 判断Cookie是否存在
	 */
	static function is_set($name) {
		return isset($_COOKIE[C('COOKIE_PREFIX').$name]);
	}

	/**
	 * 获取某个Cookie值
	 */
	static function get($name) {

		return Cookie::is_set($name) ? $_COOKIE[C('COOKIE_PREFIX').$name] : '';
	}

	/**
	 * 设置某个Cookie值
	 */
	static function set($name, $value, $expire='', $path='', $domain='') {
		if($expire=='') {
			$expire	=	C('COOKIE_EXPIRE');
		}
		if(empty($path)) {
			$path = C('COOKIE_PATH');
		}
		$expire	= !empty($expire) ?	time()+$expire : 0;
		if(empty($domain)) {
            $domain	=	C('COOKIE_DOMAIN');
		    $host=$_SERVER['HTTP_HOST'];
            $host = strtolower($host);
            if(strstr($host,"qieke.cn")!==false){
    		    $bear_type=$_SERVER['HTTP_X_UP_BEAR_TYPE'];
    		    $line_id=$_SERVER['HTTP_X_UP_CALLING_LINE_ID'];
    		    if ($bear_type && $line_id)
    		        $domain="";
    		    else
                    $domain=".qieke.cn";
            }
            if(strstr($host,".uvan.com")!==false){
                $domain=".uvan.com";
            }
        }
		setcookie(C('COOKIE_PREFIX').$name, $value, $expire, $path, $domain);
		$_COOKIE[C('COOKIE_PREFIX').$name] = $value;
	}

	/**
	 * 删除某个Cookie值
	 */
	static function delete($name) {
		Cookie::set($name,'',-1);
		unset($_COOKIE[C('COOKIE_PREFIX').$name]);
	}

	/**
	 * 清空Cookie值
	 */
	static function clear() {
		unset($_COOKIE);
	}
}
